package myreflect3;

import java.lang.reflect.Constructor;

public class ReflectDemo1 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException {
        // Constructor<?>] getConstructors():                                    返回所有公共构造方法对象的数组
        // method1();

        // Constructor<?>[] getDeclaredConstructors():                           返回所有构造方法对象的数组
        // method2();

        // Constructor<T> getConstructor(Class<?>...parameterTypes):             返回单个公共构造方法对象
        // method3();

        // Constructor<T> getDeclaredConstructor (class<?>... parameterTypes):   返回单个构造方法对象
        method4();

    }

    private static void method4() throws ClassNotFoundException, NoSuchMethodException {
        // Constructor<T> getDeclaredConstructor (class<?>... parameterTypes):   返回单个构造方法对象
        Class<?> clazz = Class.forName("myreflect3.Student");
        System.out.println(clazz.getDeclaredConstructor(String.class));
    }

    private static void method3() throws ClassNotFoundException, NoSuchMethodException {
        // Constructor<T> getConstructor(Class<?>...parameterTypes):             返回单个公共构造方法对象
        Class<?> clazz = Class.forName("myreflect3.Student");
        // 小括号中,一定要跟构造方法的形参保持一致
        System.out.println(clazz.getConstructor());
        System.out.println(clazz.getConstructor(String.class, int.class));
    }

    private static void method2() throws ClassNotFoundException {
        // Constructor<?>[] getDeclaredConstructors():                           返回所有构造方法对象的数组
        Class<?> clazz = Class.forName("myreflect3.Student");
        for (Constructor<?> constructor : clazz.getDeclaredConstructors()) {
            System.out.println(constructor);
        }
    }

    private static void method1() throws ClassNotFoundException {
        // Constructor<?>] getConstructors():                                    返回所有公共构造方法对象的数组
        Class<?> clazz = Class.forName("myreflect3.Student");
        for (Constructor<?> constructor : clazz.getConstructors()) {
            System.out.println(constructor);
        }
    }
}
